package com.example.demo01minio.controller;

import com.example.demo01minio.service.FileService;
import io.minio.errors.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * ClassName:FileController
 * Package:com.example.demo01minio.controller
 * Description:
 *
 * @Author:helei
 * @Create:2024/8/27 - 9:36
 */
@RestController
@RequestMapping("/admin/file")
//swagger描述接口模块的注解
@Tag(name = "文件管理模块")
public class FileController {

    @Autowired
    private FileService fileService;

    //http://localhost:5174/admin/file/upload
    //Request Method:
    //POST
    //描述接口的注解
    @Operation(summary = "文件上传")
    //1、文件上传接口
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        //上传文件后返回文件的路径 给前端回显
        return fileService.upload(file);
    }

    //2、删除文件接口
    //http://192.168.144.128:9000/stgy/image/2024/08/27/1724727808636_1fbc6b.jpg
    @Operation(summary = "文件删除")
    @DeleteMapping("/delete")
    //in = ParameterIn.QUERY 表示swagger文档中，该参数描述时 参数传参的位置是哪里
    //in = ParameterIn.PATH 路径参数 HEADER头
    public String delete(@Parameter(in = ParameterIn.QUERY,
            required = true,
            description = "要删除图片文件的完整路径",
            example = "http://192.168.144.128:9000/stgy/image/")
                         @RequestParam("path") String path) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return fileService.delete(path);
    }

}
